package com.kung.qvs.data;

import com.kung.qvs.data.source.HttpDataSource;
import com.kung.qvs.data.source.LocalDataSource;
import com.kung.qvs.entity.BaseResultBean;
import com.kung.qvs.entity.DemoEntity;
import com.kung.qvs.entity.GarbageTypeBean;
import com.kung.qvs.entity.KRecordWeight;
import com.kung.qvs.entity.ProductBean;
import com.kung.qvs.entity.ProductInfoBean;
import com.kung.qvs.entity.ProductTypeBean;
import com.kung.qvs.entity.PropInfoBean;
import com.kung.qvs.entity.ResSetInfoBean;
import com.kung.qvs.entity.StatisticsBean;
import com.kung.qvs.entity.WorkerBean;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;

import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.http.BaseResponse;
import me.goldze.mvvmhabit.http.BaseResponseBean;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * MVVM的Model层，统一模块的数据仓库，包含网络数据和本地数据（一个应用可以有多个Repositor）
 * Created by goldze on 2019/3/26.
 */
public class JtRepository extends BaseModel implements HttpDataSource, LocalDataSource {
    private volatile static JtRepository INSTANCE = null;
    private final HttpDataSource mHttpDataSource;

    private final LocalDataSource mLocalDataSource;

    private JtRepository(@NonNull HttpDataSource httpDataSource,
                         @NonNull LocalDataSource localDataSource) {
        this.mHttpDataSource = httpDataSource;
        this.mLocalDataSource = localDataSource;
    }

    public static JtRepository getInstance(HttpDataSource httpDataSource,
                                           LocalDataSource localDataSource) {
        if (INSTANCE == null) {
            synchronized (JtRepository.class) {
                if (INSTANCE == null) {
                    INSTANCE = new JtRepository(httpDataSource, localDataSource);
                }
            }
        }
        return INSTANCE;
    }

    @VisibleForTesting
    public static void destroyInstance() {
        INSTANCE = null;
    }




    @Override
    public void saveUserName(String userName) {
        mLocalDataSource.saveUserName(userName);
    }

    @Override
    public void savePassword(String password) {
        mLocalDataSource.savePassword(password);
    }

    @Override
    public String getUserName() {
        return mLocalDataSource.getUserName();
    }

    @Override
    public String getPassword() {
        return mLocalDataSource.getPassword();
    }

    @Override
    public void saveAccount(String account) {
        mLocalDataSource.saveAccount(account);
    }

    @Override
    public String getAccount() {
        return mLocalDataSource.getAccount();
    }

    @Override
    public void saveLoginState(Boolean state) {
        mLocalDataSource.saveLoginState(state);
    }

    @Override
    public boolean getLoginState() {
        return mLocalDataSource.getLoginState();
    }

    @Override
    public void saveKeepPwd(Boolean state) {
        mLocalDataSource.saveKeepPwd(state);
    }

    @Override
    public boolean getKeepPwd() {
        return mLocalDataSource.getKeepPwd();
    }

    @Override
    public void saveBaseUrl(String baseUrl) {
        mLocalDataSource.saveBaseUrl(baseUrl);
    }

    @Override
    public String getBaseUrl() {
        return mLocalDataSource.getBaseUrl();
    }

    @Override
    public void saveConvertGreyImgByFloyd(String convertGreyImgByFloyd) {
        mLocalDataSource.saveConvertGreyImgByFloyd(convertGreyImgByFloyd);
    }

    @Override
    public String getConvertGreyImgByFloyd() {
        return mLocalDataSource.getConvertGreyImgByFloyd();
    }

    @Override
    public void saveEquipmentNo(String equipmentNo) {
        mLocalDataSource.saveEquipmentNo(equipmentNo);
    }

    @Override
    public String getEquipmentNo() {
        return mLocalDataSource.getEquipmentNo();
    }

    @Override
    public void saveOriginPlace(String originPlace) {
        mLocalDataSource.saveOriginPlace(originPlace);
    }

    @Override
    public String getOriginPlace() {
        return mLocalDataSource.getOriginPlace();
    }

    @Override
    public void saveProducer(String producer) {
        mLocalDataSource.saveProducer(producer);
    }

    @Override
    public String getProducer() {
        return mLocalDataSource.getProducer();
    }

    @Override
    public void saveContact(String contact) {
        mLocalDataSource.saveContact(contact);
    }

    @Override
    public String getContact() {
        return mLocalDataSource.getContact();
    }

    @Override
    public void saveTare(String tare) {
        mLocalDataSource.saveTare(tare);
    }

    @Override
    public String getTare() {
        return mLocalDataSource.getTare();
    }

    @Override
    public void saveCompanyName(String companyName) {
        mLocalDataSource.saveCompanyName(companyName);
    }

    @Override
    public String getCompanyName() {
        return mLocalDataSource.getCompanyName();
    }

    @Override
    public void saveProduct(String product) {
        mLocalDataSource.saveProduct(product);
    }

    @Override
    public String getProduct() {
        return mLocalDataSource.getProduct();
    }

    @Override
    public void saveLoginType(String loginType) {
        mLocalDataSource.saveLoginType(loginType);
    }

    @Override
    public String getLoginType() {
        return mLocalDataSource.getLoginType();
    }

    @Override
    public Observable<BaseResultBean<List<ProductTypeBean>>> reqSynchronizeProductTypes(String orgCode, String deviceCode) {
        return mHttpDataSource.reqSynchronizeProductTypes(orgCode, deviceCode);
    }

    @Override
    public Observable<BaseResultBean<List<ProductBean>>> reqSynchronizeProducts(String orgCode, String deviceCode) {
        return mHttpDataSource.reqSynchronizeProducts(orgCode, deviceCode);
    }

    @Override
    public Observable<BaseResultBean<Integer>> reqAddProductBean(String orgCode, String deviceCode, String productCode, String parentId,
                                                                 String productName, String productWeight, String convertRate, String remark) {
        return mHttpDataSource.reqAddProductBean(orgCode, deviceCode, productCode, parentId, productName, productWeight, convertRate, remark);
    }

    @Override
    public Observable<BaseResultBean<Integer>> reqUpdateProductBean(String orgCode, String deviceCode, String productCode, String parentId,
                                                                    String productName, String productWeight, String convertRate, String remark) {
        return mHttpDataSource.reqUpdateProductBean(orgCode, deviceCode, productCode, parentId, productName, productWeight, convertRate, remark);
    }

    @Override
    public Observable<BaseResultBean<Integer>> reqDeleteProductBean(String orgCode, String deviceCode, String productCode) {
        return mHttpDataSource.reqDeleteProductBean(orgCode, deviceCode, productCode);
    }

    @Override
    public Observable<BaseResultBean<List<WorkerBean>>> reqSynchronizePackers(String orgCode, String deviceCode, String roleId, String userName) {
        return mHttpDataSource.reqSynchronizePackers(orgCode, deviceCode, roleId, userName);
    }

    @Override
    public Observable<BaseResultBean<Integer>> reqDeletePacker(String orgCode, String deviceCode, String userId) {
        return mHttpDataSource.reqDeletePacker(orgCode, deviceCode, userId);
    }

    @Override
    public Observable<BaseResultBean<Integer>> reqAddPacker(String orgCode, String deviceCode, String userCode, String userName) {
        return mHttpDataSource.reqAddPacker(orgCode, deviceCode, userCode, userName);
    }

    @Override
    public Observable<BaseResultBean<Integer>> reqUpdatePacker(String orgCode, String deviceCode, String userId,
                                                               String userCode, String userName) {
        return mHttpDataSource.reqUpdatePacker(orgCode, deviceCode, userId, userCode, userName);
    }

    @Override
    public Observable<BaseResultBean<KRecordWeight>> reqUploadWeightRecord(String orgCode, String deviceCode, String productId,
                                                                           String productName, String standardWeight,
                                                                           String convertRate, String packerId, String packerAccount,
                                                                           String packerName, String weight, String amount,
                                                                           String printCompany, String isHang) {
        return mHttpDataSource.reqUploadWeightRecord(orgCode, deviceCode, productId, productName, standardWeight, convertRate,
                packerId, packerAccount, packerName, weight, amount, printCompany, isHang);
    }

    @Override
    public Observable<BaseResultBean<List<KRecordWeight>>> reqGetWeightRecord(String orgCode, String deviceCode, String workerId,
                                                                              String beginTime, String endTime,
                                                                              int pageSize, int pageIndex) {
        return mHttpDataSource.reqGetWeightRecord(orgCode, deviceCode, workerId, beginTime, endTime, pageSize, pageIndex);
    }

    @Override
    public Observable<BaseResultBean<StatisticsBean>> reqGetWeightRecordStatistics(String orgCode, String deviceCode) {
        return mHttpDataSource.reqGetWeightRecordStatistics(orgCode, deviceCode);
    }

    @Override
    public Observable<BaseResultBean<List<KRecordWeight>>> reqGetWeightRecordStatisticsDetail(String orgCode, String deviceCode
            , String beginTime, String endTime) {
        return mHttpDataSource.reqGetWeightRecordStatisticsDetail(orgCode, deviceCode, beginTime, endTime);
    }
}
